using Microsoft.Extensions.Caching.StackExchangeRedis;
using ZhaoXi.MSACommerce.Authentication.JwtBearer;
using Zhaoxi.MSACommerce.CategoreService.HttpApi;
using Zhaoxi.MSACommerce.CategoryService.Infrastructure;
using Zhaoxi.MSACommerce.CategoryService.UseCases;
using Zhaoxi.MSACommerce.HttpApi.Common;
using ZiggyCreatures.Caching.Fusion;
using ZiggyCreatures.Caching.Fusion.Serialization.SystemTextJson;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.


// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

builder.Services.AddInfrastructure(builder.Configuration);

builder.Services.AddUseCase();
builder.Services.AddFusionCache()
    .WithDistributedCache(
        new RedisCache(new RedisCacheOptions
        {
            Configuration = builder.Configuration.GetConnectionString("RedisConnection")
        }))
    .WithDefaultEntryOptions(options => 
    {
        options.Duration = TimeSpan.FromMinutes(30);
        options.IsFailSafeEnabled = true;
    })
    .WithSerializer(new FusionCacheSystemTextJsonSerializer());

builder.Services.AddHttpApi();
builder.Services.AddControllers();
builder.Services.AddJwtBearer(builder.Configuration);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseHttpCommon();

app.MapControllers();

app.Run();
